﻿using SurfaceAd.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Windows.ApplicationModel;
using Windows.Foundation;
using Windows.Storage;

/**
 * 创建时间：2014/9/22 18:39:23
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace SurfaceAd.Assistant
{
    public sealed class PackageConfigHelper
    {
        private async Task<String> GetAppManifestAsync()
        {
            StorageFile storageFile = await Package.Current.InstalledLocation.GetFileAsync(Constants.AppManifest);
            string manifestXml = await FileIO.ReadTextAsync(storageFile);
            return manifestXml;
        }

        private String GetDocumentAsync()
        {
            return GetAppManifestAsync().Result;
        }

        private async Task<String> GetAwaitDocumentAsync()
        {
            return await Task.Factory.StartNew(() => GetDocumentAsync());
        }

        public IAsyncOperation<Boolean> InitAsyncOperation()
        {
            return this.Init().AsAsyncOperation();
        }

        private async Task<Boolean> Init()
        {
            String content = await GetAwaitDocumentAsync();
            if (string.IsNullOrWhiteSpace(content))
                return false;
            XDocument doc = null;
            try
            {
                doc = XDocument.Parse(content);
            }
            catch { }
            if (!(doc != null && doc.Root != null))
                return false;

            //        
            #region 节点读取
            foreach (XElement element in doc.Root.Elements())
            {
                if (element == null)
                    continue;
                string elementName = element.Name.LocalName;
                if (string.IsNullOrWhiteSpace(elementName))
                    continue;
                //
                LogHelper.LogInfo("------------" + elementName);
            }
            #endregion

            return true;
        }
    }
}
